import axios from 'axios'
import router from '@/router'
import store from '../store'
import {Notification} from 'element-ui'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'production' ? "" : '/api',
  timeout: 20 * 1000, // 请求超时时间
  loading: true
})

// request拦截器
service.interceptors.request.use(
    config => {
      document.getElementById('loading_panel').style.display = 'block'
      if (store.getters.author_token) config.headers.common['Authorization'] = store.state.author_token
      if (store.getters.access_token) config.headers.common['authorization'] = store.state.access_token
      if (store.getters.refresh_token) config.headers.common['authorization'] = config.headers.common['authorization'] + ';' + store.state.refresh_token
      config.headers['Content-Type'] = 'application/json'
      return config
    },
    error => {
      // Do something with request error
      document.getElementById('loading_panel').style.display = 'none'
      console.log("request=error", error) // for debug
      Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
      document.getElementById('loading_panel').style.display = 'none'
      const code = response.status
      if (code < 200 || code > 300) {
        Notification.error({
          title: response.message
        })
        return Promise.reject('error')
      } else {
        return response.data
      }
    },
    error => {
      document.getElementById('loading_panel').style.display = 'none'
      let code = 0
      try {
        code = error.response.data.status
      } catch (e) {
        if (error.toString().indexOf('Error: timeout') !== -1) {
          Notification.error({
            title: '网络请求超时',
            duration: 5000
          })
          return Promise.reject(error)
        }
      }
      if (code) {
        if (code === 401) {
          // const userInfo = JSON.parse(store.getters.userInfo)
          // store.dispatch('LogOut', userInfo.id).then(() => {
          //   location.reload()
          // })
        } else if (code === 403) {
          router.push({path: '/401'})
        } else {
          const errorMsg = error.response.data.message
          if (errorMsg !== undefined) {
            Notification.error({
              title: errorMsg,
              duration: 5000
            })
          }
        }
      } else {
        Notification.error({
          // title: '接口请求失败',
          title: '服务器繁忙，请稍后再试!',
          duration: 5000
        })
      }
      return Promise.reject(error)
    }
)
export default service
